import { createRouter, createWebHistory } from 'vue-router'
import Login from '../components/Login.vue'
import TodoApp from '../components/TodoApp.vue'
import UserCenter from '../components/UserCenter.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/user',
      component: UserCenter
    },
    {
      path: '/todo',
      component: TodoApp
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const currentUser = localStorage.getItem('currentUser')
  if (to.path !== '/login' && !currentUser) {
    next('/login')
  } else {
    next()
  }
})

export default router 